package com.ynkbny.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ynkbny.base.PageResult;
import com.ynkbny.domain.Role;
import com.ynkbny.domain.RouterLow;
import com.ynkbny.domain.SystemLog;
import com.ynkbny.domain.User;
import com.ynkbny.mapper.UserMapper;
import com.ynkbny.service.impl.RoleAndRouterServiceImpl;
import com.ynkbny.service.impl.RoleServiceImpl;
import com.ynkbny.service.impl.RouterServiceImpl;
import com.ynkbny.util.Res;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

/**
 * @author zelin.wang
 * @description 角色信息controller
 * @date 8/16/2023
 */
@Slf4j
@RestController
@Api(tags = "角色信息")
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleServiceImpl roleService;
    @Resource
    RouterServiceImpl routerService;
    @Resource
    private UserMapper userMapper;
    @Resource
    RoleAndRouterServiceImpl roleAndRouterService;

    /**
     * @param role 角色信息
     * @return 结果
     * @author : zelinwang
     */
    @PostMapping("/edit")
    public Res<Boolean> editRole(Role role) {
        log.info("请求参数：{}", role);
        boolean b = roleService.updateById(role);
        log.info("返回值：{}", b);
        return Res.ok(b);
    }

    /**
     * @param role 角色信息
     * @return 结果
     * @author : zelinwang
     */
    @DeleteMapping("/delete")
    public Res<Boolean> delRole(Role role) {
        log.info("请求参数：{}", role);
        boolean b = roleService.removeById(role);
        log.info("返回值：{}", b);
        return Res.ok(b);
    }

    /**
     * @param role 角色信息
     * @return 结果
     * @author : zelinwang
     */
    @PutMapping("/add")
    public Res<Boolean> addRole(Role role) {
        log.info("请求参数：{}", role);
        boolean b = roleService.save(role);
        log.info("返回值：{}", b);
        return Res.ok(b);
    }

    /**
     * @return 结果
     * @author : zelinwang
     */
    @GetMapping("/getAll")
    public Res<List<Role>> getAll() {
        log.info("请求参数：无");
        Subject subject = SecurityUtils.getSubject();
        String userName = (String) subject.getPrincipal();
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserName, userName));
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        if (user.getRoleId().equals("4")){
            roleQueryWrapper.in("id",1,4);
        }
        List<Role> list = roleService.list(roleQueryWrapper);
        log.info("返回值：{}", list);
        return Res.ok(list);
    }

    /**
     * @param routerId id
     * @param role     角色信息
     * @return 结果
     * @author : zelinwang
     */
    @PostMapping("/editWithRouter")
    public Res<Boolean> editWithRouter(Role role, List<Integer> routerId) {
        log.info("请求参数：无");
        Boolean aBoolean = roleService.editWithRouter(role, routerId);
        log.info("返回值：{}", aBoolean);
        return Res.ok(aBoolean);
    }

    /**
     * @return 结果
     * @author : zelinwang
     */
    @GetMapping("/getAllRouter")
    public Res<List<RouterLow>> getAllRouter() {
        log.info("请求参数：无");
        List<RouterLow> list = routerService.list();
        log.info("返回值：{}", list);
        return Res.ok(list);
    }

    /**
     *
     * @param pageNum 页条数
     * @param pageSize 页数
     * @param condition 高级查询
     * @return 分页数据
     */
    @GetMapping("/pageList")
    public Res<PageResult<Role>> getSystemLog(int pageNum, int pageSize,String condition) {
        return Res.ok(roleService.pageList(condition,pageNum,pageSize));
    }
}
